local oldAngles = false

local function CreateMove (cmd)
	if tgm.VMmuldir < 0 then
		oldAngles = false
		return
	end
	
	if not (tgm.CurrentGame and tgm.CurrentGame.MouseDisabled) then
		tgm.mouse.x = math.ceil (math.Clamp (tgm.mouse.x + cmd:GetMouseX() * tgm.MouseSensitivity, tgm.MouseInsetLimit, tgm.ScreenSize - tgm.MouseInsetLimit))
		tgm.mouse.y = math.ceil (math.Clamp (tgm.mouse.y + cmd:GetMouseY() * tgm.MouseSensitivity, tgm.MouseInsetLimit, tgm.ScreenSize - tgm.MouseInsetLimit))
	end
	
	if tgm.CVAR_MouseMovesVM:GetBool() then
		tgm.VMAngAdd.x = (tgm.VMAngAdd.x / (1 + FrameTime() * tgm.VMAngAddDecaySpeed)) - cmd:GetMouseY() * tgm.VMAngAddImpact
		tgm.VMAngAdd.y = (tgm.VMAngAdd.y / (1 + FrameTime() * tgm.VMAngAddDecaySpeed)) - cmd:GetMouseX() * tgm.VMAngAddImpact
	end
	
	if oldAngles then cmd:SetViewAngles (oldAngles) end
	oldAngles = cmd:GetViewAngles()
end

hook.Add ("CreateMove", "TGM.CreateMove", CreateMove)

function tgm:DrawMouse ()
	if tgm.VMmuldir < 0 then return end --never render while the toolgun isn't under control
	
	if tgm.CurrentGame and tgm.CurrentGame.MouseDisabled then return end
	
	surface.SetDrawColor (255, 255, 255, 255)
	local x, y = math.floor( tgm.mouse.x ), math.floor( tgm.mouse.y )
	surface.DrawLine (x - tgm.MouseSize, y, x + tgm.MouseSize, y)
	surface.DrawLine (x, y - tgm.MouseSize, x, y + tgm.MouseSize)
end
